﻿using System;
using System.Collections.Generic;
using System.Text;
using IDotNet.MapLibs.Model;

namespace IDotNet.MapLibs.BMap
{
    public class BMapLib : IMapLib
    {

        protected double lw(double a, double b, double c)
        {
            a = Math.Max(a, b);
            a = Math.Min(a, c);
            return a;
        }

        protected double ew(double a, double b, double c)
        {
            while (a > c)
                a -= c - b;
            while (a < b)
                a += c - b;
            return a;
        }

        protected double oi(double a)
        {
            return Math.PI * a / 180;
        }

        protected double Td(double a, double b, double c, double d)
        {
            return 6370996.81 * Math.Acos(Math.Sin(c) * Math.Sin(d) + Math.Cos(c) * Math.Cos(d) * Math.Cos(b - a));
        }

        protected double Wv(PointModel a, PointModel b)
        {
            a.lng = ew(a.lng, -180, 180);
            a.lat = lw(a.lat, -74, 74);
            b.lng = ew(b.lng, -180, 180);
            b.lat = lw(b.lat, -74, 74);
            return Td(oi(a.lng), oi(b.lng), oi(a.lat), oi(b.lat));
        }

        public double GetDistance(PointModel a, PointModel b)
        {
            var c = Wv(a, b);
            return c;
        }

    }
}
